package link_list

// https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii leetcode-82
func deleteDuplicates(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}

	dummyNode := &ListNode{Next: head}
	prev := dummyNode
	for node := head; ; {
		if node == nil {
			prev.Next = nil
			break
		}

		if node.Next != nil && node.Val == node.Next.Val {
			currVal := node.Val
			for ; node != nil && node.Val == currVal; node = node.Next {
			}
			continue
		} else {
			prev.Next = node
			prev = node
		}
		node = node.Next
	}
	return dummyNode.Next
}
